<?php
class Administrator_song_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }	
	
	function content_management_add()
	{
		$is_publish = $this->input->post('is_publish');
		$song_publish_date = $this->input->post('song_publish_date');
		$media_type_id = $this->input->post('media_type_id');
		$genre_id = $this->input->post('genre_id');
		$genre_tag = $this->input->post('genre_tag');
		$is_show_hot_on_chart = $this->input->post('is_show_hot_on_chart');
		$is_show_hurt_song_mood = $this->input->post('is_show_hurt_song_mood');
		$is_show_love_song_mood = $this->input->post('is_show_love_song_mood');
		$is_show_new_released = $this->input->post('is_show_new_released');
		$is_show_oldie = $this->input->post('is_show_oldie');
		$is_show_ost_song_mood = $this->input->post('is_show_ost_song_mood');
		$is_show_play_song = $this->input->post('is_show_play_song');
		$is_show_recommend = $this->input->post('is_show_recommend');
		$letters = $this->input->post('letters');
		$song_name = $this->input->post('song_name');
		$song_description = $this->input->post('song_description');
		$song_lyrics = $this->input->post('song_lyrics');
		$song_embed = $this->input->post('song_embed');
		$song_tag = $this->input->post('song_tag');
		$artist_id = $this->input->post('artist_id');
		$default_artist_id = $this->input->post('default_artist_id');
		$album_id = $this->input->post('album_id');
		$default_album_id = $this->input->post('default_album_id');
		$video_id = $this->input->post('video_id');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');
		
		//Insert song
		$data = array(
			'media_type_id' => $media_type_id,
			'song_publish_date' => convertDatetime($song_publish_date),
			'is_publish' => $is_publish,
			'is_show_hot_on_chart' => $is_show_hot_on_chart,
			'is_show_hurt_song_mood' => $is_show_hurt_song_mood,
			'is_show_love_song_mood' => $is_show_love_song_mood,
			'is_show_new_released' => $is_show_new_released,
			'is_show_oldie' => $is_show_oldie,
			'is_show_ost_song_mood' => $is_show_ost_song_mood,
			'is_show_play_song' => $is_show_play_song,
			'is_show_recommend' => $is_show_recommend,
			'letters' => $letters,
			'update_by' => $this->session->userdata('username'),
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('song',$data);
		$song_id = $this->db->insert_id();
		
		//Update You2Play Intrend
		if(!empty($is_show_intrend)) $this->db->update('song',array('is_show_intrend' => '0'),array('song_id !=' => $song_id));
		
		//Insert song_info
		$data = array(
			'song_id' => $song_id,
			'song_name' => $song_name,
			'song_description' => $song_description,
			'song_lyrics' => $song_lyrics,
			'is_youtube' => (preg_match('/youtube/',$song_embed) ? '1':'0'),
			'song_embed' => $song_embed,
			'genre_tag' => $genre_tag,			
			'song_tag' => $song_tag
		);
		$this->db->insert('song_info',$data);	
		
		//Update song_file
		$dataFile = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			$dateY = date('Y',strtotime($datetime)).'/';
			$dateM = date('m',strtotime($datetime)).'/';
			$dateD = date('d',strtotime($datetime)).'/';
			$source_image = $this->temp_folder.$value;
			$path_info= pathinfo($source_image);
			$new_path = $this->upload_folder.'song/files/';
			@mkdir($new_path.$dateY);
			@mkdir($new_path.$dateY.$dateM);
			@mkdir($new_path.$dateY.$dateM.$dateD);
			$new_path = $new_path.$dateY.$dateM.$dateD;
			$file_name= md5($song_id.rand());
			$new_image= $new_path.$file_name.'.'.$path_info['extension'];
			while(file_exists(realpath($new_image))):
				$file_name = md5($song_id.rand());
				$new_image = $new_path.$file_name.'.'.$path_info['extension'];
			endwhile;			
			@copy($source_image,$new_image); //Put File to original
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataFile = array(
				'song_file_name' => $file_name,
				'song_file_ext' => $path_info['extension']
			);	
		endforeach;
		if(!empty($dataFile)) $this->db->update('song_info',$dataFile,array('song_id' => $song_id));	
		
		//Insert song_artist
		$dataArtistID = array();
		if(!empty($artist_id)) foreach($artist_id as $key => $value):
			$dataArtistID[] = array(
				'song_id' => $song_id,
				'artist_id' => $value,
				'is_default' => (in_array($value,$default_artist_id) ? '1':'0')				
			);
		endforeach;
		if(!empty($dataArtistID)) $this->db->insert_batch('song_artist',$dataArtistID);
		
		//Insert song_album
		$dataAlbumID = array();
		if(!empty($album_id)) foreach($album_id as $key => $value):
			$dataAlbumID[] = array(
				'song_id' => $song_id,
				'album_id' => $value,
				'is_default' => (in_array($value,$default_album_id) ? '1':'0')		
			);
		endforeach;
		if(!empty($dataAlbumID)) $this->db->insert_batch('song_album',$dataAlbumID);
		
		/*Insert song_genre*/
		$dataSongGenre = array();
		if(!empty($genre_id)) foreach($genre_id as $value):
			$dataSongGenre[] = array(
				'genre_id' => $value,
				'song_id' => $song_id
			);
		endforeach;
		if(!empty($dataSongGenre)) $this->db->insert_batch('song_genre',$dataSongGenre);
		
		//Insert Video_soong
		if(!empty($video_id)):
			//Delete video_song
			$this->db->delete('video_song',array('song_id' => $song_id));		
			//Delete video_artist
			$this->db->delete('video_artist',array('video_id' => $video_id[0]));
			//Insert Video song
			$this->db->insert('video_song',array('video_id' => $video_id[0],'song_id' => $song_id));
			//Update Video
			$this->db->update('video',array('is_relate_song' => '1'),array('video_id' => $video_id[0]));
		else:
			//Delete video_song
			$this->db->delete('video_song',array('song_id' => $song_id));						
		endif;
		
		//Update Solr
		$this->load->library('solr');	
		
		$document = new Apache_Solr_Document();
		$document->id = 'song_'.$song_id;
		$document->song_name_s = $song_name;
		$document->is_ost_song_i = $is_show_ost_song_mood;
		$document->song_tag_s = $song_tag;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));
		if(!empty($song_publish_date)) $document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime(convertDatetime($song_publish_date)));
		
		$artist_id_i_arr = array();
		$artist_name_s = array();
		if(!empty($artist_id)) foreach($artist_id as $key => $value):
			$artist_id_i_arr[] = $value;
			//Get Artist Name
			$dataArtist = $this->db->select('artist_name')->get_where('artist_info',array('artist_id' => $value),1)->row();
			$artist_name_s[] = $dataArtist->artist_name;
		endforeach;
		$document->artist_id_i_arr = $artist_id_i_arr;
		$document->song_artist_name_s = implode($artist_name_s,',');
		
		$album_id_i_arr = array();
		$album_name_s = array();
		if(!empty($album_id)) foreach($album_id as $key => $value):
			$album_id_i_arr[] = $value;
			if($key==0){
				$dataSongImage = $this->db->select('j.album_file_name,j.album_file_ext')
				->join('album_info as j','j.album_id=t.album_id','INNER')
				->get_where('song_album as t',array('t.song_id' => $song_id,'t.is_default' => '1'),1)->row();
				if(!empty($dataSongImage)) $document->song_image_name_s = $dataSongImage->album_file_name.'.'.$dataSongImage->album_file_ext;
			}
			//Get Album name
			$dataAlbum = $this->db->select('album_name')->get_where('album_info',array('album_id' => $value->album_id),1)->row();
			$album_name_s[] = $dataAlbum->album_name;			
		endforeach;
		$document->album_id_i_arr = $album_id_i_arr;
		$document->song_album_name_s = implode($album_name_s,',');
		
		if($this->solr->ping() && !empty($document)):
			$this->solr->addDocument($document);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;	
		
		//Clear Memcache
		$this->memcache->delete('dataHomeSongThai');
		$this->memcache->delete('dataHomeSongInter');
		$this->memcache->delete('dataHomeSongAsian');
		$this->memcache->delete('dataHomeSongAsian');
		$this->memcache->delete('dataSongList');
		$this->memcache->delete('dataSongNewRelease');
		$this->memcache->delete('dataSongRecommend');
		$this->memcache->delete('dataSongHit');
	}
	
	function content_management_edit($song_id)
	{
		$is_publish = $this->input->post('is_publish');
		$song_publish_date = $this->input->post('song_publish_date');
		$media_type_id = $this->input->post('media_type_id');
		$genre_id = $this->input->post('genre_id');
		$genre_tag = $this->input->post('genre_tag');
		$is_show_hot_on_chart = $this->input->post('is_show_hot_on_chart');
		$is_show_hurt_song_mood = $this->input->post('is_show_hurt_song_mood');
		$is_show_love_song_mood = $this->input->post('is_show_love_song_mood');
		$is_show_new_released = $this->input->post('is_show_new_released');
		$is_show_oldie = $this->input->post('is_show_oldie');
		$is_show_ost_song_mood = $this->input->post('is_show_ost_song_mood');
		$is_show_play_song = $this->input->post('is_show_play_song');
		$is_show_recommend = $this->input->post('is_show_recommend');
		$letters = $this->input->post('letters');
		$song_name = $this->input->post('song_name');
		$song_description = $this->input->post('song_description');
		$song_lyrics = $this->input->post('song_lyrics');
		$song_embed = $this->input->post('song_embed');
		$song_tag = $this->input->post('song_tag');
		$artist_id = $this->input->post('artist_id');
		$default_artist_id = $this->input->post('default_artist_id');
		$album_id = $this->input->post('album_id');
		$default_album_id = $this->input->post('default_album_id');
		$video_id = $this->input->post('video_id');
		$chords_id = $this->input->post('chords_id');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');
		
		/*Update user*/
		$data = array(
			'media_type_id' => $media_type_id,
			'song_publish_date' => convertDatetime($song_publish_date),
			'is_publish' => $is_publish,
			'is_show_hot_on_chart' => $is_show_hot_on_chart,
			'is_show_hurt_song_mood' => $is_show_hurt_song_mood,
			'is_show_love_song_mood' => $is_show_love_song_mood,
			'is_show_new_released' => $is_show_new_released,
			'is_show_oldie' => $is_show_oldie,
			'is_show_ost_song_mood' => $is_show_ost_song_mood,
			'is_show_play_song' => $is_show_play_song,
			'is_show_recommend' => $is_show_recommend,
			'letters' => $letters,
			'update_by' => $this->session->userdata('username'),
			'update_date' => $datetime
		);
		$this->db->update('song',$data,array('song_id' => $song_id));
		
		//Update You2Play Intrend
		if(!empty($is_show_intrend)) $this->db->update('song',array('is_show_intrend' => '0'),array('song_id !=' => $song_id));		
		
		/*Update song_info*/
		$data = array(
			'song_id' => $song_id,
			'song_name' => $song_name,
			'song_description' => $song_description,
			'song_lyrics' => $song_lyrics,
			'is_youtube' => (preg_match('/youtube/',$song_embed) ? '1':'0'),			
			'song_embed' => $song_embed,
			'genre_tag' => $genre_tag,
			'song_tag' => $song_tag
		);
		$this->db->update('song_info',$data,array('song_id' => $song_id));
		
		//Update song_file
		$dataFile = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			$dataInfo = $this->db->select('t.*,j.create_date')
			->join('song as j','j.song_id=t.song_id','INNER')
			->get_where('song_info as t',array('t.song_id' => $song_id),1)->row();
					
			$dateY = date('Y',strtotime($dataInfo->create_date)).'/';
			$dateM = date('m',strtotime($dataInfo->create_date)).'/';
			$dateD = date('d',strtotime($dataInfo->create_date)).'/';
			$source_image = $this->temp_folder.$value;
			$path_info= pathinfo($source_image);
			$new_path = $this->upload_folder.'song/files/';
			@mkdir($new_path.$dateY);
			@mkdir($new_path.$dateY.$dateM);
			@mkdir($new_path.$dateY.$dateM.$dateD);
			$new_path = $new_path.$dateY.$dateM.$dateD;
			$file_name= md5($song_id.rand());
			$new_image= $new_path.$file_name.'.'.$path_info['extension'];
			while(file_exists(realpath($new_image))):
				$file_name = md5($song_id.rand());
				$new_image = $new_path.$file_name.'.'.$path_info['extension'];
			endwhile;		
			@copy($source_image,$new_image); //Put File to original
			
			//delete temp		
			@unlink($this->upload_folder.'song/files/'.$dateY.$dateM.$dateD.$dataInfo->song_file_name.'.'.$dataInfo->song_file_ext);
			@unlink($this->temp_folder.$value);
			
			$dataFile = array(
				'song_file_name' => $file_name,
				'song_file_ext' => $path_info['extension']
			);	
		endforeach;
		if(!empty($dataFile)) $this->db->update('song_info',$dataFile,array('song_id' => $song_id));	
		
		//Delete song_artist
		$this->db->delete('song_artist',array('song_id' => $song_id));
		//Insert song_artist
		$dataArtistID = array();
		if(!empty($artist_id)) foreach($artist_id as $key => $value):
			$dataArtistID[] = array(
				'song_id' => $song_id,
				'artist_id' => $value,
				'is_default' => (in_array($value,$default_artist_id) ? '1':'0')
			);
		endforeach;
		if(!empty($dataArtistID)) $this->db->insert_batch('song_artist',$dataArtistID);

		//Delete song_album
		$this->db->delete('song_album',array('song_id' => $song_id));		
		//Insert song_album
		$dataAlbumID = array();
		if(!empty($album_id)) foreach($album_id as $key => $value):
			$dataAlbumID[] = array(
				'song_id' => $song_id,
				'album_id' => $value,
				'is_default' => (in_array($value,$default_album_id) ? '1':'0')				
			);
		endforeach;
		if(!empty($dataAlbumID)) $this->db->insert_batch('song_album',$dataAlbumID);
		
		/*Insert song_genre*/
		$dataSongGenre = array();
		$this->db->delete('song_genre',array('song_id' => $song_id)); //Delete First
		if(!empty($genre_id)) foreach($genre_id as $value):
			$dataSongGenre[] = array(
				'genre_id' => $value,
				'song_id' => $song_id
			);
		endforeach;
		if(!empty($dataSongGenre)) $this->db->insert_batch('song_genre',$dataSongGenre);		
		
		//Insert Video_soong
		if(!empty($video_id)):
			//Delete video_song
			$this->db->delete('video_song',array('song_id' => $song_id));		
			//Delete video_artist
			$this->db->delete('video_artist',array('video_id' => $video_id[0]));
			//Insert Video song
			$this->db->insert('video_song',array('video_id' => $video_id[0],'song_id' => $song_id));
			//Update Video
			$this->db->update('video',array('is_relate_song' => '1'),array('video_id' => $video_id[0]));			
		else:
			//Delete video_song
			$this->db->delete('video_song',array('song_id' => $song_id));						
		endif;
		
		//Update Chord Relate With Song
		if(!empty($chords_id)):
			$this->db->update('chords',array('song_id' => $song_id),array('chords_id' => $chords_id[0]));
		else:
			$this->db->update('chords',array('song_id' => NULL),array('song_id' => $song_id));				
		endif;		
		
		//Update Solr
		$this->load->library('solr');	
		
		$document = new Apache_Solr_Document();
		$document->id = 'song_'.$song_id;
		$document->song_name_s = $song_name;
		$document->is_ost_song_i = $is_show_ost_song_mood;
		$document->song_tag_s = $song_tag;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));
		if(!empty($song_publish_date)) $document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime(convertDatetime($song_publish_date)));	
		
		$artist_id_i_arr = array();
		$artist_name_s = array();
		if(!empty($artist_id)) foreach($artist_id as $key => $value):
			$artist_id_i_arr[] = $value;
			//Get Artist Name
			$dataArtist = $this->db->select('artist_name')->get_where('artist_info',array('artist_id' => $value),1)->row();
			$artist_name_s[] = $dataArtist->artist_name;
		endforeach;
		$document->artist_id_i_arr = $artist_id_i_arr;
		$document->song_artist_name_s = implode($artist_name_s,',');
		
		$album_id_i_arr = array();
		if(!empty($album_id)) foreach($album_id as $key => $value):
			$album_id_i_arr[] = $value;
			if($key==0){
				$dataSongImage = $this->db->select('j.album_file_name,j.album_file_ext')
				->join('album_info as j','j.album_id=t.album_id','INNER')
				->get_where('song_album as t',array('t.song_id' => $song_id,'t.is_default' => '1'),1)->row();
				if(!empty($dataSongImage)) $document->song_image_name_s = $dataSongImage->album_file_name.'.'.$dataSongImage->album_file_ext;
			}
		endforeach;
		$document->album_id_i_arr = $album_id_i_arr;
		
		if($this->solr->ping() && !empty($document)):
			$this->solr->addDocument($document);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;
		
		//Clear Memcache
		$this->memcache->delete('dataHomeSongThai');
		$this->memcache->delete('dataHomeSongInter');
		$this->memcache->delete('dataHomeSongAsian');
		$this->memcache->delete('dataHomeSongAsian');
		$this->memcache->delete('dataSongList');
		$this->memcache->delete('dataSongNewRelease');
		$this->memcache->delete('dataSongRecommend');
		$this->memcache->delete('dataSongHit');						
	}	
	
	function content_management_delete()
	{
		$id = $this->input->post('id');
		$ids = array();
		foreach($id as $value):
			$ids[] = "song_".$value;
			$dataInfo = $this->db->select('t.*,j.create_date')
			->join('song as j','j.song_id=t.song_id','INNER')
			->get_where('song_info as t',array('t.song_id' => $value),1)->row();
			$tables = array('song','song_info','song_artist','song_album','video_song','song_click_stat','song_rating');
			$this->db->where('song_id', $value);
			$this->db->delete($tables);
			//Delete Files
			$dateY = date('Y',strtotime($dataInfo->create_date)).'/';
			$dateM = date('m',strtotime($dataInfo->create_date)).'/';
			$dateD = date('d',strtotime($dataInfo->create_date)).'/';
			@unlink($this->upload_folder.'song/files/'.$dateY.$dateM.$dateD.$dataInfo->song_file_name.'.'.$dataInfo->song_file_ext);
		endforeach;	
		
		//Update Solr
		$this->load->library('solr');	
		if($this->solr->ping() && !empty($ids)):
			$this->solr->deleteByMultipleIds($ids);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;
		
		//Clear Memcache
		$this->memcache->delete('dataHomeSongThai');
		$this->memcache->delete('dataHomeSongInter');
		$this->memcache->delete('dataHomeSongAsian');
		$this->memcache->delete('dataHomeSongAsian');
		$this->memcache->delete('dataSongList');
		$this->memcache->delete('dataSongNewRelease');
		$this->memcache->delete('dataSongRecommend');
		$this->memcache->delete('dataSongHit');	
						
	}
}

?>